<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Text</title>
</head>
<body> 
  <style>
    #box{
      width: 200px;
      height: 200px;
      background-color: rgb(71, 152, 228);
    }
    #box1{
      margin-top: 20px;
      width: 200px;
      height: 200px;
      background-color: rgb(190, 90, 140);
    }
  </style>
  <div id="box">
    
  </div>
  <div id="box1">
    
  </div>
  <script type="text/javascript">
    // 防抖
    // 单位时间内频繁触发，只执行最后一次
    let i=0
    function debounce(fn,t) {
      let timer
      return function () {
        if(timer) clearTimeout(timer)
        timer=setTimeout(fn,t)
      }
    }
    let box = document.getElementById('box')
    function moveMounce() {
      i++
      box.innerHTML=i
    }
    box.addEventListener('mousemove',debounce(moveMounce,1000))

    // 节流函数
    // 单位时间内频繁触发只执行一次
    let j=0
    function throttle(fn,t) {
      let timer=null
      return function () {
        if(!timer){
          timer=setTimeout(()=>{
            fn()
            timer=null
          },t)
        }
      }
    }
    let box1 = document.getElementById('box1')
    function moveMounce1() {
      j++
      box1.innerHTML=j
    }
    box1.addEventListener('mousemove',throttle(moveMounce1,1000))
  </script>
</body>
</html>